package cn.pegasus.teamwork.appinterface.controller.washer;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;

import cn.iyizhan.teamwork.base.util.ObjectUtil;
import cn.iyizhan.teamwork.base.util.PasswordEncoder;
import cn.pegasus.teamwork.appinterface.base.BaseController;
import cn.pegasus.teamwork.appinterface.controller.user.vo.usr.ReturnRegisterVo;
import cn.pegasus.teamwork.appinterface.controller.washer.vo.ReturnRegisterWasherVo;
import cn.pegasus.teamwork.appinterface.session.MySessionContext;
import cn.pegasus.teamwork.appinterface.util.ParameterEunm;
import cn.pegasus.teamwork.appinterface.util.ReturnRsUtils;
import cn.pegasus.teamwork.appinterface.util.SLEmojiFilterUtils;
import cn.pegasus.teamwork.appinterface.util.ValidatorDataUtil;
import cn.pegasus.teamwork.pegasus.entity.BussWasherFundstatistics;
import cn.pegasus.teamwork.pegasus.entity.BussWasherInfo;
import cn.pegasus.teamwork.pegasus.service.BussUcenterAreaService;
import cn.pegasus.teamwork.pegasus.service.BussUserOrderService;
import cn.pegasus.teamwork.pegasus.service.BussWasherFundstatisticsService;
import cn.pegasus.teamwork.pegasus.service.BussWasherInfoService;
import cn.pegasus.teamwork.pegasus.service.BussWasherSginService;
import cn.pegasus.teamwork.sysconfig.entity.SysConfigDictionary;
import cn.pegasus.teamwork.sysconfig.service.SysConfigDictionaryService;
import cn.pegasus.teamwork.sysconfig.service.SysConfigMessageService;
import cn.pegasus.teamwork.sysconfig.service.SysConfigSmsService;
import cn.pegasus.teamwork.weixinpay.Constant;

/**
 * @ClassName: SysUcenerUserController
 * @Description: 用户控制中心
 * @author ryan.bian
 * @date 2017年11月12日 上午10:10:32
 * 
 *       包括以下主要几个接口：
 * 
 *       1.用户注册 2.用户登录 3.用户第三方接口登录 3.用户修改资料 4.用户手机校验（是否存在唯一性） 5.用户密码修改
 *       6.用户支付密码修改 7.用户退出
 * 
 * 
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
@RequestMapping("/wsr/*")
public class WasherInfoController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(WasherInfoController.class);

	@Autowired
	private BussWasherInfoService washerInfoService;

	@Autowired
	private BussWasherFundstatisticsService washerFundstatisticsService;

	@Autowired
	private SysConfigSmsService configSmsService;

	@Autowired
	private BussWasherSginService washerSginService;

	@Autowired
	private SysConfigDictionaryService dictionaryService;

	@Autowired
	private BussUcenterAreaService areaService;

	/**
	 * service
	 */
	@Autowired
	private BussUserOrderService bussUserOrderService;

	@Autowired
	private SysConfigMessageService messageService;
	
	/**
	 * 用户注册接口nfc
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/reg", method = RequestMethod.POST)
	public Map<String, Object> registerWasher(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		ReturnRegisterVo vo = new ReturnRegisterVo();
		// 是否为空参数校验
		boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "washerMobile", "washerProtocolimage",
				"washerPassword", "washerJobnumber", "washerRealname", "washerIsEntrust", "vcode" }, map);
		if (isNotNull) {
			String userMobile = map.get("washerMobile").toString();
			if (ValidatorDataUtil.isMobile(userMobile)) {
				// TODO 验证码校验
				String userPassword = map.get("washerPassword").toString();
				userPassword = PasswordEncoder.getPassword(userPassword);
				Map<String, Object> nap = new HashMap<String, Object>();
				nap.put("washerMobile", userMobile);

				BussWasherInfo es = washerInfoService.detail(nap);
				if (es == null) {
					nap.put("userPassword", userPassword);
					nap.put("washerStatus", 2);
					nap.put("washerType", 10);
					nap.put("washerExamine", 2);
					nap.put("washerFrom", 1);
					nap.put("washerCode", "FMDS_" + userMobile);
					nap.put("washerName", "FMDS_" + userMobile);
					nap.put("washerIsdeposit", 2);
					nap.put("washerIsdepEntrust", 2);
					BussWasherInfo newuses = washerInfoService.add(nap);
					if (newuses != null) {
						// 开通资金总账户
						BussWasherFundstatistics bs = new BussWasherFundstatistics();
						bs.setWasherId(Long.valueOf(newuses.getId().toString()));
						washerFundstatisticsService.add(bs);

						// 将数据存储到session中
						HttpSession session = request.getSession();
						session.setAttribute("washers", newuses);
						MySessionContext.getInstance().addSession(session);

						setSessionWasher(newuses);

						vo.setUserMobile(userMobile);
						vo.setUid(newuses.getId().toString());
						vo.setUserCertificate(getSession().getId());

						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, vo);
					} else {
						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE, null);
					}

				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, null);
				}
			}
		} else {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_BANK_PARAMS_CODE, null);
		}

		return resultMap;
	}

	/**
	 * 用户通过微信登录之后数据绑定
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/wb", method = RequestMethod.POST)
	public Map<String, Object> washerBrand(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 是否为空参数校验
		boolean isNotNull = ObjectUtil.checkObjectFile(
				new String[] { "washerMobile", "washerProtocolimage", "washerRealname", "washerIsEntrust",
						"receivaName", "receivaAccount", "receivaBank", "serviceAddressId", "vcode", "vcodeType" },
				map);
		if (isNotNull) {
			String userMobile = map.get("washerMobile").toString();
			long userId = getSessionWasherId();
			String code = map.get("vcode").toString();
			int vcodeType = Integer.valueOf(map.get("vcodeType").toString());
			boolean rs = configSmsService.checkSms(Long.valueOf(userMobile), userId, vcodeType, code);
			if (rs) {
				// 数据绑定，更新数据
				Map<String, Object> nnap = new HashMap<String, Object>();
				nnap.put("id", getSessionWasherId());
				nnap.put("washerMobile", map.get("washerMobile"));
				BussWasherInfo es1 = new BussWasherInfo();
				es1 = washerInfoService.detail(nnap);
				if (es1 != null) {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_USER_ISEXIST_CODE, false);
				} else {
					BussWasherInfo es = new BussWasherInfo();
					es.setId(getSessionWasherId().intValue());
					es.setWasherMobile(map.get("washerMobile").toString());
					// es.setWasherJobnumber(map.get("washerJobnumber").toString());
					es.setWasherJobnumber(String.valueOf(System.currentTimeMillis()));
					es.setWasherRealname(map.get("washerRealname").toString());
					es.setWasherProtocolimage(map.get("washerProtocolimage").toString());
					es.setWasherIsEntrust(Integer.valueOf(map.get("washerIsEntrust").toString()));
					es.setReceivaName(map.get("receivaName").toString());
					es.setReceivaAccount(map.get("receivaAccount").toString());
					es.setReceivaBank(map.get("receivaBank").toString());
					es.setAreaId(Integer.valueOf(map.get("serviceAddressId").toString()));
					es.setWasherExamine(2);
					if(map.get("washerPhonecardimage") != null){
						es.setWasherPhonecardimage(map.get("washerPhonecardimage").toString());
					}
					if(map.get("washerScphonecardimage") != null){
						es.setWasherScphonecardimage(map.get("washerScphonecardimage").toString());
					}
					washerInfoService.update(es);
					
					configSmsService.sendWasherRegNotice(es.getWasherMobile());
					messageService.addMessage(2,"6", "提交资料审核", "尊敬的 " + map.get("washerRealname").toString()
					+"，您的个人信息申请资料正在审核中，审核时间需1-3个工作日，请您耐心等待，咨询电话：0755-27805678，谢谢！",
					es.getId().toString());
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);
				}
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PHONE_CODE, false);
			}
		} else {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_BANK_PARAMS_CODE, null);
		}

		return resultMap;
	}

	/**
	 * 用户登录
	 * 
	 * @param userName
	 * @param password
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/lu", method = RequestMethod.POST)
	public Map<String, Object> loginUser(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			ReturnRegisterVo vo = new ReturnRegisterVo();
			// 是否为空参数校验
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "washerMobile", "washerPassword" }, map);
			if (isNotNull) {
				String userMobile = map.get("washerMobile").toString();
				String userPassword = map.get("washerPassword").toString();
				userPassword = PasswordEncoder.getPassword(userPassword);
				map.put("washerPassword", userPassword);
				if (ValidatorDataUtil.isMobile(userMobile)) {
					BussWasherInfo newuse = washerInfoService.detail(map);
					if (newuse != null) {
						// 将数据存储到session中
						HttpSession session = request.getSession();
						session.setAttribute("washers", newuse);
						MySessionContext.getInstance().addSession(session);
						setSessionWasher(newuse);

						vo.setUserMobile(userMobile);
						vo.setUcode(newuse.getWasherCode());
						vo.setUid(newuse.getId().toString());
						vo.setUserCertificate(getSession().getId());

						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, vo);

					} else {
						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE, null);
					}
				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_BANK_PARAMS_CODE, null);
				}
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_BANK_PARAMS_CODE, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * 用户第三方接口登录
	 * 
	 * @param id
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/tlu")
	public Map<String, Object> thirdLoginUser(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		ReturnRegisterWasherVo vo = new ReturnRegisterWasherVo();
		try {
			// 是否为空参数校验
			boolean isNotNull = ObjectUtil
					.checkObjectFile(new String[] { "wxUuid", "wxOpenid", "washerNick", "washerImages" }, map);
			Map<String, Object> rMap = new HashMap<String, Object>();
			rMap.put("dictName", "washerTel");

			String washerNick = map.get("washerNick").toString();
			
			map.put("washerNick",SLEmojiFilterUtils.filterEmoji(washerNick));
			SysConfigDictionary c = dictionaryService.detail(rMap);
			String washerTel = c.getDictValue();

			Map<String, Object> rMap2 = new HashMap<String, Object>();
			rMap2.put("dictName", "washerDepositBalance");

			SysConfigDictionary c2 = dictionaryService.detail(rMap2);
			String washerDepositBalance = c2.getDictValue();

			if (isNotNull) {
				Map<String, Object> ltMap = new HashMap<String, Object>();
				ltMap.put("wxOpenid", map.get("wxOpenid"));
				BussWasherInfo bi = washerInfoService.detail(ltMap);
				if (bi != null) {
					// 将数据存储到session中
					HttpSession session = request.getSession();
					session.setAttribute("washers", bi);
					MySessionContext.getInstance().addSession(session);
					setSessionWasher(bi);

					vo.setWasherNick(bi.getWasherNick());
					vo.setWasherImages(bi.getWasherImages());
					vo.setWcode(bi.getWasherCode());
					vo.setWid(bi.getId().toString());

					vo.setUserCertificate(getSession().getId());
					if (bi.getWasherMobile() != null) {
						vo.setWasherMobile(bi.getWasherMobile());
						vo.setIsbrandPhone("1");
					} else {
						vo.setWasherMobile("");
						vo.setIsbrandPhone("2");
					}

					// 获取洗车工的服务地址
					if(bi.getAreaId() != null){
						int areaid = bi.getAreaId();
						if (areaid > 0) {
							String areaName = areaService.getAreaName(areaid);
							if (!StringUtils.isEmpty(areaName)) {
								vo.setWasherServiceArea(areaName);
							} else {
								vo.setWasherServiceArea("");
							}
						} else {
							vo.setWasherServiceArea("");
						}
					}else {
						vo.setWasherServiceArea("");
					}
								

					Map<String, Object> tMap = new HashMap<String, Object>();
					// 是否为空参数校验
					tMap.put("washerId", bi.getId());
					int count = 0;
					count = bussUserOrderService.findOrderInfoBySpoke(tMap);
					if (count > 0) {
						vo.setWasherNotFineshCount(count);
					} else {
						vo.setWasherNotFineshCount(0);
					}

					vo.setReceivAddress(bi.getReceivAddress());
					vo.setWasherRealname(bi.getWasherRealname());
					vo.setWasherTel(washerTel);
					vo.setWasherExamine(bi.getWasherExamine());
					vo.setWasherIsdeposit(bi.getWasherIsdeposit());
					vo.setWasherIsdepEntrust(bi.getWasherIsdepEntrust());
					vo.setDepositBalance(Long.valueOf(washerDepositBalance));
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, vo);
				} else {
					String tuid = map.get("wxUuid").toString();
					map.put("washerName", "FMDS_" + tuid);
					map.put("washerStatus", 2);
					map.put("washerFrom", 3);
					map.put("washerType", 10);
					map.put("washerExamine", 2);

					BussWasherInfo bio = washerInfoService.add(map);
					if (bio != null) {
						// 开通资金总账户
						BussWasherFundstatistics bs = new BussWasherFundstatistics();
						bs.setId(Long.valueOf(bio.getId()));
						bs.setWasherId(Long.valueOf(bio.getId()));
						washerFundstatisticsService.add(bs);

						// 将数据存储到session中
						HttpSession session = request.getSession();
						session.setAttribute("washers", bio);
						MySessionContext.getInstance().addSession(session);

						setSessionWasher(bio);

						vo.setWasherNick(bio.getWasherNick());
						vo.setWasherImages(bio.getWasherImages());
						vo.setWcode(bio.getWasherCode());
						vo.setWid(bio.getId().toString());

						vo.setUserCertificate(getSession().getId());
						if (bio.getWasherMobile() != null) {
							vo.setWasherMobile(bio.getWasherMobile());
							vo.setIsbrandPhone("1");
						} else {
							vo.setWasherMobile("");
							vo.setIsbrandPhone("2");
						}
						vo.setWasherTel(washerTel);

						// 获取洗车工的服务地址
						if( bio.getAreaId() != null){
							int areaid = bio.getAreaId();
							if (areaid > 0) {
								String areaName = areaService.getAreaName(areaid);
								if (!StringUtils.isEmpty(areaName)) {
									vo.setWasherServiceArea(areaName);
								} else {
									vo.setWasherServiceArea("");
								}
							} else {
								vo.setWasherServiceArea("");
							}
						}else {
							vo.setWasherServiceArea("");
						}
						
						Map<String, Object> tMap = new HashMap<String, Object>();
						// 是否为空参数校验
						tMap.put("washerId", bio.getId());
						int count = 0;
						count = bussUserOrderService.findOrderInfoBySpoke(tMap);
						if (count > 0) {
							vo.setWasherNotFineshCount(count);
						} else {
							vo.setWasherNotFineshCount(0);
						}

						vo.setReceivAddress(bio.getReceivAddress());
						vo.setWasherRealname(bio.getWasherRealname());
						vo.setWasherExamine(bio.getWasherExamine());
						vo.setWasherIsdeposit(bio.getWasherIsdeposit());
						vo.setWasherIsdepEntrust(bio.getWasherIsdepEntrust());
						vo.setDepositBalance(Long.valueOf(washerDepositBalance));
						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, vo);
					}
				}
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, true);
			}
		} catch (Exception e) {
			LOGGER.error("修改用户失败.", e);
		}
		LOGGER.info("返回参数 ：{}", JSONObject.toJSON(resultMap));
		return resultMap;
	}

	/**
	 * 此接口提供用户修改资料（修改昵称，修改头像，绑定手机）
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/uu", method = RequestMethod.POST)
	public Map<String, Object> updateUser(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		int id = Integer.valueOf(getSessionWasherId().toString());
		if (id > 0) {
			String userMobile = map.get("washerMobile").toString();
			long userId = getSessionWasherId();
			String code = map.get("vcode").toString();
			int vcodeType = Integer.valueOf(map.get("vcodeType").toString());
			boolean rs = configSmsService.checkSms(Long.valueOf(userMobile), userId, vcodeType, code);
			if (rs) {
				map.put("id", id);
				washerInfoService.update(map);
			}
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);
		} else {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_BANK_PARAMS_CODE, false);
		}

		return resultMap;
	}

	/**
	 * 用户密码修改、用户支付密码修改、忘记登录密码、忘记支付密码
	 * 
	 * @param id
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/uup", method = RequestMethod.POST)
	public Map<String, Object> updateUserPwd(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 是否为空参数校验 userNick/userImages/userPassword/userPaypwd/userMobile
		int type = Integer.valueOf(map.get("type").toString());
		int id = Integer.valueOf(getSessionWasherId().toString());
		int vcodeType = Integer.valueOf(map.get("washerPassword").toString());
		BussWasherInfo bio = getSessionWasher();
		if (id > 0) {
			if (type == 1) {
				boolean isNotNull = ObjectUtil
						.checkObjectFile(new String[] { "washerPassword", "newWasherPassword", "vcode" }, map);
				if (isNotNull) {
					String userMobile = bio.getWasherMobile();
					String code = map.get("vcode").toString();
					boolean rs = configSmsService.checkSms(Long.valueOf(userMobile), Long.valueOf(id), vcodeType, code);
					if (rs) {
						BussWasherInfo bu = new BussWasherInfo();
						Map<String, Object> rtMap = new HashMap<String, Object>();
						rtMap.put("id", id);

						String userPassword = map.get("washerPassword").toString();
						userPassword = PasswordEncoder.getPassword(userPassword);
						bu.setWasherPassword(userPassword);
						;
						rtMap.put("washerPassword", userPassword);

						bu = washerInfoService.detail(rtMap);
						if (bu != null) {
							String newUserPassword = map.get("newWasherPassword").toString();
							newUserPassword = PasswordEncoder.getPassword(newUserPassword);
							rtMap.put("washerPassword", newUserPassword);
							washerInfoService.update(rtMap);
							resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);
						} else {
							resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, false);
						}
					} else {
						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PHONE_CODE, false);
					}

				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_BANK_PARAMS_CODE, false);
				}
			} else if (type == 3) {
				boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "forgetWasherPassword", "vcode" }, map);
				if (isNotNull) {
					String userMobile = bio.getWasherMobile();
					String code = map.get("vcode").toString();
					boolean rs = configSmsService.checkSms(Long.valueOf(userMobile), Long.valueOf(id), vcodeType, code);
					if (rs) {
						Map<String, Object> rtMap = new HashMap<String, Object>();
						rtMap.put("id", id);

						String newUserPassword = map.get("forgetWasherPassword").toString();
						newUserPassword = PasswordEncoder.getPassword(newUserPassword);
						rtMap.put("washerPassword", newUserPassword);

						washerInfoService.update(rtMap);
						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);

					} else {
						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PHONE_CODE, false);
					}
				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_BANK_PARAMS_CODE, false);
				}
			}
		} else {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_BANK_PARAMS_CODE, null);
		}

		return resultMap;
	}

	/**
	 * 用户手机校验（是否存在唯一性）
	 * 
	 * @param userMobile_number
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cp", method = RequestMethod.POST)
	public Map<String, Object> checkuserMobile(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 是否为空参数校验
		boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "washerMobile" }, map);
		if (isNotNull) {
			String userMobile = map.get("washerMobile").toString();
			if (ValidatorDataUtil.isMobile(userMobile)) {
				// TODO 验证码校验
				BussWasherInfo newuses = washerInfoService.detail(map);
				if (newuses != null) {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, false);
				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);
				}
			}
		} else {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_BANK_PARAMS_CODE, null);
		}

		return resultMap;
	}

	/**
	 * 洗车工签到
	 * 
	 * @param userMobile_number
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/uws", method = RequestMethod.POST)
	public Map<String, Object> updateWasherSgin(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 是否为空参数校验
		boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "params" }, map);
		if (isNotNull) {
			long washerId = getSessionWasherId();
			map.put("washerId", washerId);
			int result = washerSginService.updateWasherSginList(map);
			if (result == 0) {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, false);
			} else if (result == 1) {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_BANK_PARAMS_CODE, false);
			} else if (result == -1) {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, false);
			}
		} else {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_BANK_PARAMS_CODE, null);
		}

		return resultMap;
	}

	/**
	 * 查询洗车工打卡记录
	 * 
	 * @param userMobile_number
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/gsl", method = RequestMethod.POST)
	public Map<String, Object> getWasherSginList(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 是否为空参数校验
		long washerId = getSessionWasherId();
		map.put("washerId", washerId);
		Map<String, Object> tMap = washerSginService.getWasherSginList(map);
		if (tMap != null) {
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("lis", tMap);
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, result);
		} else {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE, false);
		}
		return resultMap;
	}

	/**
	 * 查询洗车工未开始订单数据量
	 * 
	 * @param userMobile_number
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/nfc", method = RequestMethod.POST)
	public Map<String, Object> getWasherOrderCount(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 是否为空参数校验
		long washerId = getSessionWasherId();
		map.put("washerId", washerId);
		int count = 0;
		try {
			count = bussUserOrderService.findOrderInfoBySpoke(map);
			if (count > 0) {
				Map<String, Object> result = new HashMap<String, Object>();
				result.put("count", count);
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, result);
			} else {
				Map<String, Object> result = new HashMap<String, Object>();
				result.put("count", count);
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE, result);
			}
		} catch (ClientException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return resultMap;
	}

	/**
	 * 退出账户
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/eu", method = RequestMethod.POST)
	public Map<String, Object> exitUser(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		HttpSession session = request.getSession();
		// 将数据存储到session中
		String sessionId = request.getParameter("userCertificate");
		MySessionContext.getInstance().DelSession(session);
		HttpSession nsession = MySessionContext.getInstance().getSession(sessionId);
		if (nsession == null) {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);
		} else {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, null);
		}
		return resultMap;
	}

	/**
	 * 获取用户的TOKEN
	 * 
	 * @param userMobile_number
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/gat", method = RequestMethod.POST)
	public Map<String, Object> getAccessToken(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		// 是否为空参数校验
		boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "code" }, map);
		if (isNotNull) {
			try {
				HttpClient client = new HttpClient();
				// 设置代理服务器地址和端口

				String APPID = Constant.APP_ID_WASHER;
				String SECRET = Constant.APP_SECRET_WASHER;
				String access_result = "";
				String aturl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + APPID + "&secret=" + SECRET
						+ "&grant_type=authorization_code&code=" + map.get("code");

				// 使用 GET 方法 ，如果服务器需要通过 HTTPS 连接，那只需要将下面 URL 中的 http 换成 https
				HttpMethod method = new GetMethod(aturl);
				// 使用POST方法
				// HttpMethod method = new PostMethod("http://java.sun.com");
				client.executeMethod(method);

				// 打印服务器返回的状态
				System.out.println(method.getStatusLine());
				if (method.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
					// 打印返回的信息
					System.out.println(method.getResponseBodyAsString());
					Map<String, String> m = (Map<String, String>) JSON.parse(method.getResponseBodyAsString());
					// TODO
					String access_token = m.get("access_token");
					String openid = m.get("openid");

					if (access_token != null && openid != null) {
						String url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid="
								+ openid + "&lang=zh_CN";

						// 使用 GET 方法 ，如果服务器需要通过 HTTPS 连接，那只需要将下面 URL 中的 http 换成
						// https
						HttpMethod method2 = new GetMethod(url);
						// 使用POST方法
						client.executeMethod(method2);

						// 打印服务器返回的状态
						System.out.println(method2.getStatusLine());
						if (method2.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
							// 打印返回的信息
							System.out.println(method2.getResponseBodyAsString());
							access_result = method2.getResponseBodyAsString();
							String json = new String(access_result.getBytes("ISO-8859-1"), "UTF-8");
							Map<String, Object> tMap = new HashMap<String, Object>();
							tMap.put("rs", json);

							resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, tMap);
						}
					} else {
						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE, null);
					}

				}
				// 释放连接
				method.releaseConnection();

			} catch (IOException e) {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, null);
			}
		} else {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, false);
		}

		return resultMap;
	}

	public static String filterEmoji(String source) {
        if (source == null) {
            return source;
        }
        Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
                Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
        Matcher emojiMatcher = emoji.matcher(source);
        if (emojiMatcher.find()) {
            source = emojiMatcher.replaceAll("*");
            return source;
        }
        return source;
    }
}
